#-*- coding: utf-8 -*-
from pandac.PandaModules import *
from pmcore.basecom import BaseComponent

class GUIExtNPEditor (BaseComponent):
    name = "NodePath Properties Editor"
    weight = 10
    version = "0.1"
    requires = "toolbar-control;tab-control;input-field;text-button"
    base_prop_fields = []
    _cur_np_list = None
    
    def __init__(self):
        self.provides = ({"name":"npeditor-set-np",
                        "value":self.set_active_np,
                        "version":"0.1"}, )
    
    def after_direct_import(self):
        inpfield = get_service("input-field")
        self.base_prop_fields.append(inpfield.new("X:\t",onText = self.set_props))
        self.base_prop_fields.append(inpfield.new("Y:\t",onText = self.set_props))
        self.base_prop_fields.append(inpfield.new("Z:\t",onText = self.set_props))
        self.base_prop_fields.append(inpfield.new("H:\t",onText = self.set_props))
        self.base_prop_fields.append(inpfield.new("P:\t",onText = self.set_props))
        self.base_prop_fields.append(inpfield.new("R:\t",onText = self.set_props))
        self.base_prop_fields.append(inpfield.new("SX:\t",onText = self.set_props))
        self.base_prop_fields.append(inpfield.new("SY:\t",onText = self.set_props))
        self.base_prop_fields.append(inpfield.new("SZ:\t",onText = self.set_props))
        
        txtbtn = get_service("text-button")
        self.btnRGC = txtbtn.new("reset NP center to geom center", self.reset_gcenter)
        
        tabctrl = tabctrl = get_service("tab-control")
        tabctrl.add_tab("NP Prop.")
        for elt in self.base_prop_fields:
            tabctrl.add_element(elt, "NP Prop.")
        tabctrl.add_element(self.btnRGC, "NP Prop.")
        
        add_listener('np-editor-sellistener','np-selector-select',self.set_active_np)
        
    def set_active_np(self,np):
        self._cur_np_list = np
        self.show_props()
    
    def set_props(self,*args,**kwargs):
        props = map(self.to_float,[x.value for x in self.base_prop_fields])
        if self._cur_np_list:
            for np in self._cur_np_list:
                sprops = [np.setX, np.setY, np.setZ, 
                        np.setH, np.setP, np.setR,
                        np.setSx, np.setSy, np.setSz]
                for i in xrange(len(sprops)):
                    sprops[i](props[i])
                        
    def show_props(self, *args, **kwargs):
        if self._cur_np_list:
            if len(self._cur_np_list) == 1:
                np = self._cur_np_list[0]
                gprops = [np.getX(), np.getY(), np.getZ(), 
                        np.getH(), np.getP(), np.getR(),
                        np.getSx(), np.getSy(), np.getSz()]
                print gprops
                props = [x.value for x in self.base_prop_fields]
                for i in xrange(len(props)):
                    self.base_prop_fields[i].value = gprops[i]
        else:
            for x in self.base_prop_fields:
                x.value = 0
                
    def to_float(self,val):
        try:
            res = float(val)
        except Exception:
            res = 0
        return res
    
    def reset_gcenter(self, *args, **kwargs):
        for np in self._cur_np_list:
            pt1,pt2 = np.getTightBounds()
            gcenter = (pt1+pt2)*0.5
            gcenter = gcenter - np.getPos()
            np.setPos(-gcenter)
            np.flattenLight()
            np.setPos(gcenter)
            self.show_props()
